// 获取sequelize 实例
const { sequelize } = require("../model/connect");
const cravatar = require("../utils/createAvatar");
// 传入数据模型函数,并的到 UserInfo 模型
const { UserInfo } = require("../model/models")(sequelize);

// 增 注册成功 传入用户模型
const createUserInfo = async (UserModal) => {
    await UserInfo.create({
        UserId: UserModal.id,
        avatar: cravatar(UserModal.username),
        nickname: UserModal.username,
        email: UserModal.username + "@zhentao.edu.com",
        desc: "这个人很懒，什么也没有留下",
        gender: 0,
    });
};

const getUserInfoByUserId = async (ctx) => {
    const { UserId } = ctx.params;
    try {
        const userInfo = await UserInfo.findOne({ where: { UserId } });
        if (!userInfo) {
            ctx.status = 400;
            ctx.body = {
                code: 10006,
                message: "校验失败",
                data: null,
            };
            return;
        }
        ctx.status = 200;
        ctx.body = {
            code: 10000,
            message: "获取个人信息成功",
            data: userInfo,
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            code: 10005,
            message: "服务器错误",
            data: error.message,
        };
    }
};

const updateUserInfo = async (ctx) => {
    const { UserId } = ctx.params;
    const { nickname, email, desc, gender, age } = ctx.request.body;
    console.log(gender);

    try {
        const userInfo = await UserInfo.findOne({ where: { UserId } });
        if (!userInfo) {
            ctx.status = 400;
            ctx.body = {
                code: 10007,
                message: "用户信息不存在",
                data: null,
            };
            return;
        }
        // 找到了，如果要修改的字段未传值，就使用原值
        userInfo.nickname = nickname || userInfo.nickname;
        userInfo.email = email || userInfo.email;
        userInfo.desc = desc || userInfo.desc;
        userInfo.age = age || userInfo.age;
        // 性别为 0 1，无法区分是否传值，可以判断是否为undefined确认是否传值
        userInfo.gender = gender !== undefined ? gender : userInfo.gender;
        // save 如果直接修改原对象，可以直接save保存
        userInfo.save();
        ctx.status = 200;
        ctx.body = {
            code: 10000,
            message: "修改个人信息成功",
            data: userInfo,
        };
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            code: 10005,
            message: "服务器错误",
            data: error.message,
        };
    }
};

// 修改UserId用户信息的头像内容为路径信息
const updateAvatar = async (ctx) => {
    const { UserId } = ctx.params;
    const file = ctx.file;
    // 如果没有文件就返回错误
    if (!file) {
        ctx.status = 400;
        ctx.body = {
            code: 10004,
            message: "头像文件不能为空",
            data: null,
        };
        return;
    }
    // 如果没有用户就返回错误
    try {
        const userInfo = await UserInfo.findOne({ where: { UserId } });
        if (!UserInfo) {
            ctx.status = 400;
            ctx.body = {
                code: 10004,
                message: "用户信息不存在",
                data: null,
            };
            return;
        }
        // 修改用户信息的值为头像路径值
        userInfo.avatar = "/uploads/" + file.filename;
        await userInfo.save();
        ctx.status = 200;
        ctx.body = {
            code: 10000,
            message: "头像更新成功",
            data: {
                UserId: userInfo.UserId,
                avatar: userInfo.avatar,
            },
        };
        return;
    } catch (error) {
        ctx.status = 500;
        ctx.body = {
            code: 10005,
            message: "服务器错误",
            data: error.message,
        };
    }
};

module.exports = {
    createUserInfo,
    getUserInfoByUserId,
    updateUserInfo,
    updateAvatar,
};
